package com.novellotus.iov.carvice.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.novellotus.iov.carvice.R;
import com.novellotus.iov.carvice.activity.ConfirmPayActivity;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;


/**
 * 微信支付回掉页面
 */
public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_pay_success);
        api = WXAPIFactory.createWXAPI(this, "wx27932ca8a5af2b66");
        api.handleIntent(getIntent(), this);
    }


    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
    }

    @Override
    public void onResp(BaseResp resp) {
        Log.d("Rich", getString(R.string.pay_result_callback_msg, String.valueOf(resp.errCode)));
        Log.d("Rich", "返回的数据" + resp.errStr + "-" + resp.transaction + resp.openId);
        switch (resp.errCode) {
            case 0:
                Intent intent = new Intent(this, ConfirmPayActivity.class);
                intent.putExtra("code", 0);
                setResult(100, intent);

                break;
            case -1:
                Intent intentF = new Intent(this, ConfirmPayActivity.class);
                intentF.putExtra("code", -1);
                setResult(100, intentF);
                break;
            case -2:
                Intent intentC = new Intent(this, ConfirmPayActivity.class);
                intentC.putExtra("code", -2);
                setResult(100, intentC);
                break;
        }
        finish();
    }
}